package com.qpl.depression.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.qpl.depression.entity.Article;
import com.qpl.depression.entity.Employee;
import com.qpl.depression.service.ArticleService;
import com.qpl.depression.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

@Controller
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private EmployeeService employeeService;

    /**
     * 跳转到文章列表页面
     *
     * @return 文章页面
     */
    @GetMapping("article")
    public String articlePage(ModelMap modelMap) {
        List<Article> articles = articleService.list(Wrappers.<Article>lambdaQuery().eq(Article::getStatus, 1).orderByDesc(Article::getUpdateTime));
        modelMap.addAttribute("article", articles);
        return "article";
    }

    /**
     * 跳转到文章详情页面
     *
     * @param id       文章id
     * @param modelMap 存放信息的map
     * @return 文章详情页
     */
    @GetMapping("/article/detail/{id}")
    public String articleDetail(@PathVariable("id") Long id, ModelMap modelMap) {
        Article articleDetail = articleService.getById(id);
        modelMap.addAttribute("articleDetail", articleDetail);
        List<Article> articles = articleService.getByRandom();
        modelMap.addAttribute("articles", articles);
        List<Employee> employees = employeeService.getByRandom();
        modelMap.addAttribute("employees", employees);
        return "articleDetail";
    }

    /**
     * 模糊查询文章-用于首页的搜索
     *
     * @param modelMap
     * @param name
     * @return
     */
    @GetMapping("search/article")
    public String searchArticlePage(ModelMap modelMap, String name) {
        List<Article> articles = articleService.list(Wrappers.<Article>lambdaQuery().like(Article::getContent, name).eq(Article::getStatus, 1).orderByDesc(Article::getUpdateTime));
        modelMap.addAttribute("article", articles);
        return "article";
    }

}
